package com.kkd.kkdapp.fragment;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.kkd.kkdapp.R;
import com.kkd.kkdapp.http.entity.selectedGroup;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.listener.OnSingleClickListener;
import com.kkd.kkdapp.utils.DialogUtils;
import com.kkd.kkdapp.view.iphoneTree.IphoneTreeView;
import com.kkd.kkdapp.view.iphoneTree.SelectedBidListAdapter;
import com.kkd.kkdapp.view.iphoneTree.SelectedBidTreeViewAdapter;
import com.kkd.kkdapp.view.iphoneTree.IphoneTreeView.IphoneTreeHeaderAdapter;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SanbiaoFrame  extends Fragment {
	
	private Activity mActivity ;
	private View mContentView;
	private static final int DATA_OK = 1;
	private static final int DATA_FAIL = 2;

	private ListView mSelectedBidListview;
	private ImageView img_null;
	private TextView tv_null;
	private LinearLayout title_line;
//	private ArrayList<TextView> groupTvs = new ArrayList<TextView>();
	private int curGroup = 0;
	private int curGroup2 = 0;
	private String group_name = "";
	private ArrayList<selectedGroup> mSelectedGroup = new ArrayList<selectedGroup>();
	private Dialog loadDialog;
	private SelectedBidListAdapter mSelectedBidListAdapter;
	private TextView groupTv1;
	private TextView groupTv2;
	private TextView groupTv3;
	private ArrayList<TextView> groupTvs = new ArrayList<TextView>();
	
	private Handler mHandle = new Handler() {
		@Override
		public void dispatchMessage(Message msg) {
			switch (msg.what) {
			case DATA_OK:
				if (loadDialog != null) {
					loadDialog.dismiss();
				}
				isScorll = false;
				title_line.setVisibility(View.VISIBLE);
				groupTv1.setText(mSelectedGroup.get(0).getBidTitle());
				groupTv1.setTag(0);
				groupTv2.setText(mSelectedGroup.get(1).getBidTitle());
				groupTv2.setTag(1);
				groupTv3.setText(mSelectedGroup.get(2).getBidTitle());
				groupTv3.setTag(2);
				mSelectedBidListAdapter.notifyDataSetChanged();	
				mSelectedBidListview.setOnScrollListener(mOnScrollListener);
				sendEmptyMessage(1000);
				img_null.setVisibility(View.GONE);
				tv_null.setVisibility(View.GONE);
				break;
			case DATA_FAIL:
				if (loadDialog != null) {
					loadDialog.dismiss();
				}
				img_null.setVisibility(View.VISIBLE);
				tv_null.setVisibility(View.VISIBLE);
				// showToast(msg.obj.toString());
				break;
			case 1000:
				if(!TextUtils.isEmpty(group_name)&&curGroup2==0){
					int groupid=0;
					for(int i=0;i<mSelectedGroup.size();i++){
						if(mSelectedGroup.get(i).getBidTitle().equals(group_name)){
							groupid = i;
							break;
						}
					}
					setSelection(groupid);
				}else{
					setSelection(curGroup2);
				}
				break;

			default:
				break;
			}
		};

	};
	
	@Override
	@Deprecated
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mActivity = activity;
	}
	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		group_name = mActivity.getIntent().getStringExtra("group_name");
		if (loadDialog != null) {
			loadDialog.cancel();
			loadDialog = null;
		}
		loadDialog = DialogUtils.toLoadDialog(mActivity, "正在为您筛选优质标的...");

		requestSelectedBid();
	}
	
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}
	private boolean isScorll = false;
	@Override
	public void onPause() {
		isScorll = false;
		curGroup2 = curGroup;
		super.onPause();
	}
	@Override
	public void onDestroy() {
		if (loadDialog != null) {
			loadDialog.cancel();
			loadDialog = null;
		}
		super.onDestroy();
	}
		
	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		mContentView = inflater.inflate(R.layout.sanbiao_frame, container,
				false);
		initView();
		return mContentView;
	}
	
	private void initView() {
		title_line =  (LinearLayout) mContentView.findViewById(R.id.title_line);
		img_null= (ImageView) mContentView.findViewById(R.id.img_null);
		tv_null= (TextView) mContentView.findViewById(R.id.tv_null);
		groupTv1 = (TextView) mContentView.findViewById(R.id.group1);
		groupTv1.setSelected(true);
		groupTv2 = (TextView) mContentView.findViewById(R.id.group2);
		groupTv2.setSelected(false);
		groupTv3 = (TextView) mContentView.findViewById(R.id.group3);
		groupTv3.setSelected(false);
		
		groupTvs.add(groupTv1);
		groupTvs.add(groupTv2);
		groupTvs.add(groupTv3);
		mSelectedBidListview = (ListView) mContentView.findViewById(R.id.selected_bid_listview);
		groupTv1.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				int tag = (Integer) v.getTag();
//				setSelectedGroup(tag);
				setSelection(tag);
			}
		});
		groupTv2.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				int tag = (Integer) v.getTag();
//				setSelectedGroup(tag);
				setSelection(tag);
			}
		});
		groupTv3.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				int tag =0;
				try {
					tag	= (Integer) v.getTag();
				} catch (Exception e) {
					// TODO: 忽略
				}			
//				setSelectedGroup(tag);
				setSelection(tag);
				
			}
		});
//		mSelectedBidListview
//				.setHeaderView(mActivity.getLayoutInflater().inflate(
//						R.layout.selected_bid_tree_group_title,
//						mSelectedBidListview, false));
//		mSelectedBidListview.setGroupIndicator(null);
//		mSelectedBidTreeViewAdapter = new SelectedBidTreeViewAdapter(mActivity,
//				mSelectedBidListview, mSelectedGroup, new IphoneTreeHeaderAdapter() {
//
//					@Override
//					public int getTreeHeaderState(int groupPosition,
//							int childPosition) {
//						return 0;
//					}
//
//					@Override
//					public void configureTreeHeader(View header,
//							int groupPosition, int childPosition, int alpha) {
//						curGroup = groupPosition;
//						if (curGroup == 0) {
//							for (int i = 0; i < groupTvs.size(); i++) {
//								groupTvs.get(i).setText(
//										mSelectedGroup.get(curGroup + i)
//												.getBidTitle());
//								groupTvs.get(i).setTag(curGroup + i);
//							}
//							groupTvs.get(0).setSelected(true);
//							groupTvs.get(1).setSelected(false);
//							groupTvs.get(2).setSelected(false);
//						} else if (curGroup == (mSelectedGroup.size() - 1)) {
//							for (int i = 0; i < groupTvs.size(); i++) {
//								groupTvs.get(i).setText(
//										mSelectedGroup.get(curGroup - 2 + i)
//												.getBidTitle());
//								groupTvs.get(i).setTag(curGroup - 2 + i);
//							}
//							groupTvs.get(0).setSelected(false);
//							groupTvs.get(1).setSelected(false);
//							groupTvs.get(2).setSelected(true);
//						} else {
//							groupTvs.get(0).setText(
//									mSelectedGroup.get(curGroup - 1)
//											.getBidTitle());
//							groupTvs.get(0).setTag(curGroup - 1);
//							groupTvs.get(1).setText(
//									mSelectedGroup.get(curGroup).getBidTitle());
//							groupTvs.get(1).setTag(curGroup);
//							groupTvs.get(2).setText(
//									mSelectedGroup.get(curGroup + 1)
//											.getBidTitle());
//							groupTvs.get(2).setTag(curGroup + 1);
//
//							groupTvs.get(0).setSelected(false);
//							groupTvs.get(1).setSelected(true);
//							groupTvs.get(2).setSelected(false);
//						}
//					}
//
//					@Override
//					public void onHeadViewClick(int groupPosition, int status) {
//						// TODO Auto-generated method stub
//
//					}
//
//					@Override
//					public int getHeadViewClickStatus(int groupPosition) {
//						// TODO Auto-generated method stub
//						return 0;
//					}
//
//					@Override
//					public int getGroupCount() {
//						// TODO Auto-generated method stub
//						return 0;
//					}
//
//				});
		mSelectedBidListAdapter = new SelectedBidListAdapter(mActivity, mSelectedGroup);
		mSelectedBidListview.setAdapter(mSelectedBidListAdapter);
		mSelectedBidListview.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				isScorll = true;
				return false;
			}
		});

	}
	
	OnScrollListener mOnScrollListener = new OnScrollListener() {
		
		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState) {
		}
		
		@Override
		public void onScroll(AbsListView view, int firstVisibleItem,
				int visibleItemCount, int totalItemCount) {
			if(isScorll){
				int vb = mSelectedBidListview.getFirstVisiblePosition();
				System.out.println("看看"+firstVisibleItem);
				int groupPosition_l=0;
				int childPosition=0;
				int size = 0;
				int size2 = 0;
				for(int i=0;i<mSelectedGroup.size();i++){
					size = size+mSelectedGroup.get(i).getBidGrounp().size()+1;
					if(i==mSelectedGroup.size()-1)
						size++;
					if(size>vb){
						groupPosition_l = i;
						childPosition = vb-size2;
						break;
					}
					size2 = size;
				}
				setSelectedGroup(groupPosition_l);
			}
			
			
		}
	};
	protected void setSelection(int tag) {
		int position = 0;
		isScorll = true;
		if(mSelectedGroup.size()>0){
			for(int i=0;i<tag;i++){
				position = position+mSelectedGroup.get(i).getBidGrounp().size()+1;
			}
			mSelectedBidListview.setSelection(position);
		}		
	}
	private void requestSelectedBid() {
		KkdHttpMagager.getInstance().selectedBid(new TypeToken<List<selectedGroup>>() {
		}.getType(),new HttpCallBack() {

			@Override
			public void onSuccess(int statusCode, Object responseBody) {
				@SuppressWarnings("unchecked")
				List<selectedGroup> data =  (List<selectedGroup>) responseBody;
				mSelectedGroup.clear();
				mSelectedGroup
						.addAll(data);
				mHandle.sendEmptyMessage(DATA_OK);
			}

			@Override
			public void onFailure(int statusCode, String desc) {
				Message msg = new Message();
				msg.what = DATA_FAIL;
				msg.obj = desc;
				mHandle.sendMessage(msg);
			}
		});

	}

	private void setSelectedGroup(int tag){
		if(mSelectedGroup.size()<=0)
			return;
		System.out.println("tag----"+tag);
		curGroup = tag;
		if (curGroup == 0) {
			for (int i = 0; i < groupTvs.size(); i++) {
				groupTvs.get(i).setText(
						mSelectedGroup.get(curGroup + i)
								.getBidTitle());
				groupTvs.get(i).setTag(curGroup + i);
			}
			groupTvs.get(0).setSelected(true);
			groupTvs.get(1).setSelected(false);
			groupTvs.get(2).setSelected(false);
		} else if (curGroup == (mSelectedGroup.size() - 1)) {
			for (int i = 0; i < groupTvs.size(); i++) {
				groupTvs.get(i).setText(
						mSelectedGroup.get(curGroup - 2 + i)
								.getBidTitle());
				groupTvs.get(i).setTag(curGroup - 2 + i);
			}
			groupTvs.get(0).setSelected(false);
			groupTvs.get(1).setSelected(false);
			groupTvs.get(2).setSelected(true);
		} else {
			groupTvs.get(0).setText(
					mSelectedGroup.get(curGroup - 1)
							.getBidTitle());
			groupTvs.get(0).setTag(curGroup - 1);
			groupTvs.get(1).setText(
					mSelectedGroup.get(curGroup).getBidTitle());
			groupTvs.get(1).setTag(curGroup);
			groupTvs.get(2).setText(
					mSelectedGroup.get(curGroup + 1)
							.getBidTitle());
			groupTvs.get(2).setTag(curGroup + 1);

			groupTvs.get(0).setSelected(false);
			groupTvs.get(1).setSelected(true);
			groupTvs.get(2).setSelected(false);
		}
	}
}
